package cn.com.dashihui.api.service;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.com.dashihui.api.base.BaseService;
import cn.com.dashihui.api.common.OrderCode;
import cn.com.dashihui.kit.DoubleKit;

public class ItaskService  extends BaseService{

	/**
	 * 根据卡号，流水号查询时段有无记录   
	 * @param acctNo
	 * @return 1  有重复记录  2没有重复记录
	 */
	public int findPayInByAcctNoBussSeqNo(String acctNo,String BussSeqNo) {
		int flag = 1;
		List<Record> findIn = Db.find("SELECT * FROM t_bus_pay_bankno WHERE AcctNo=? AND BussSeqNo=? ",acctNo,BussSeqNo);
		//转入未找到记录
		if(findIn==null||findIn.size()==0){
			 //查询提现记录，防止银行对冲
			 List<Record> findOut = Db.find("SELECT * FROM t_bus_pay_bankout WHERE AcctNo=? AND BussSeqNo=? ",acctNo,BussSeqNo);
			 //提现记录也未找到
			 if(findOut==null||findOut.size()==0){
				 //返回未找到
				 flag = 2;
			 }
		} 
		return flag;
	}
	/**
	 * 根据卡号查询时段提现流水号
	 * @param acctNo
	 * @return
	 */
//	public List<Record> findPayOutByAcctNo(String acctNo,String BussSeqNo) {
//		return Db.find("SELECT * FROM t_bus_pay_bankout WHERE AcctNo=?",acctNo);
//	}
	/**
	 * 存入记录   修改金额
	 * @return  1  操作成功，0 操作失败  2存入记录失败 3修改用户余额失败  4金额变动日志 5 银行响应信息日志
	 */
	@Before(Tx.class)
	public int savePayBankNo(int state,Record recs,Record rec,String bussSeqNo, String acctNo, String money) {
		
		Record record = new Record().set("BussSeqNo", bussSeqNo)
									.set("AcctNo", acctNo)
									.set("money", Double.parseDouble(money))
									.set("createDate", new Date());
		//如果是异常订单
		if(state == 1){
			record.set("describe", "未找到相应卡号信息,交易失败").set("state", OrderCode.OrderException.VOIDEXCEPTION);
		}
		//存入数据
		if(!Db.save("t_bus_pay_bankno", record)){
			return  2;
		}
		//如果不是异常订单  执行余额变动
		if(state!=1){
			//准备该银行卡绑定人信息
			int userid = rec.getInt("id");
				//变化前金额
			Double oldMoney = rec.getDouble("money");
				//增加的金额
			Double addMoney = Double.parseDouble(money);
				//变化后金额
			Double newMoney = DoubleKit.add(oldMoney, addMoney);
			//修改用户金额  并记录日志
			if(Db.update("UPDATE t_bus_user SET money=? WHERE id=?",newMoney ,userid)!=1){
				return 3;
			}
			Record re = new Record().set("userid", userid)
					.set("amount", addMoney)
					.set("type", 1)
					.set("describe", "充值")
					.set("fromOrderNum", bussSeqNo)
					.set("createDate", new Date())
					.set("orderType", OrderCode.MoneyState.ADDMONEY)
					.set("nowAmount", newMoney)
					.set("oldAmount", oldMoney);
			if(!Db.save("t_bus_user_money", re)){
				return 4;
			}
		}
		if(!Db.save("t_bus_pay_log_detail", recs)){
			return 5;
		}
		return 1;
	}
	public Record findUserByAcctNo(String acctNo) {
		// TODO Auto-generated method stub
		String sql = "SELECT tu.id,tu.money FROM t_bus_user_card tc "
				+ "LEFT JOIN t_bus_user tu ON tc.userid=tu.id WHERE tc.cardNo=?";
		return Db.findFirst(sql,acctNo);
	}
	
	
	/**
	 * 查询订单信息
	 * @param int1
	 * @return 
	 */
	public List<Record> findPayOrderIdByuserid() {
		return Db.find("SELECT * FROM t_bus_user_pay_orderid WHERE ischeck=0 ");
	}
	
}
